package com.tt.jshb.bill.pay;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzecool.common.utils.SizeUtils;
import com.hzecool.common.utils.ToastUtils;
import com.hzecool.core.base.TBaseActivity;
import com.hzecool.core.rxbus.RxBus;
import com.tt.jshb.R;
import com.tt.jshb.bean.FinishEvent;
import com.tt.jshb.bean.PayBean;
import com.tt.jshb.utils.BitMapUtils;
import com.tt.jshb.utils.QRCodeUtil;

import butterknife.BindView;

/**
 * Created by tu on 2018/11/29.
 */

public class QrCodePayActivity extends TBaseActivity<IPayView, PayPresenter>
        implements IPayView {
    @BindView(R.id.tv_scan)
    TextView tvScan;
    @BindView(R.id.tv_back1)
    TextView tv_back1;
    @BindView(R.id.iv)
    ImageView iv;

    @Override
    public void onLoadData(Object o) {

    }

    @Override
    public void onEmptyData() {

    }

    @Override
    public void onLoadError(String msg) {

    }

    @Override
    public void onNetError(String msg) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.pay_layout_activity;
    }

    @Override
    public void initView() {
        PayBean payBean = getIntent().getParcelableExtra("payInfo");

        Bitmap tp = QRCodeUtil.createQRCodeBitmap(payBean.getData().getUrl(), SizeUtils.dp2px(300), SizeUtils.dp2px(300));

        iv.setImageBitmap(tp);

        tvScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean ret = BitMapUtils.saveImg(tp, String.valueOf(System.currentTimeMillis()) + ".png", QrCodePayActivity.this);
                if (ret) {
                    ToastUtils.showLongToast("从相册中识别二维码支付");

                    if ("1".equals(payBean.getData().getZf())) {
                        try {
                            PackageManager packageManager
                                    = QrCodePayActivity.this.getApplicationContext().getPackageManager();
                            Intent intent = packageManager.
                                    getLaunchIntentForPackage("com.eg.android.AlipayGphone");
                            startActivity(intent);
                        } catch (Exception e) {
                            ToastUtils.showLongToast("请先安装支付宝");
                        }
                    } else {
                        try {
                            Intent intent = new Intent();
                            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
                            intent.setAction(Intent.ACTION_MAIN);
                            intent.addCategory(Intent.CATEGORY_LAUNCHER);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setComponent(cmp);
                            startActivity(intent);
                        } catch (Exception e) {
                            ToastUtils.showLongToast("请先安装微信");
                        }

                    }
                }
            }
        });

        tv_back1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                RxBus.postEvent(new FinishEvent(1), FinishEvent.class);
            }
        });
    }

    @Override
    public void initTitle(ImageView ivBack, TextView tvBack, View llBack, TextView titleName, TextView tvMenu, View titleRoot) {
        titleName.setText("扫码支付");
    }

    @Override
    protected PayPresenter createPresenter() {
        return new PayPresenter();
    }
}
